﻿using System;
using PTiRouZ.Net.SilverLight.Api;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace PTiRouZ.Net.SilverLight.Api.HelloTipi
{
    /// <summary>Objet du résultat d'une requête demandant la liste des albums d'un site de famille</summary>
    /// <see cref="site_albums"></see>
    [DataContract]
    public class site_albums
    {
        /// <summary>Identifiant de l'album</summary>
        [DataMember]
        public int id { get; set; }

        /// <summary>nom de l'album </summary>
        [DataMember]
        public string name { get; set; }

        /// <summary>vignette de l'album</summary>
        [DataMember]
        public string image_small { get; set; }

        /// <summary>Album par default ?</summary>
        [DataMember]
        public int default_album { get; set; }
        
    }

    public delegate void SiteAlbumsEventHandler(object sender, SiteAlbumsEventArgs e);
    public class SiteAlbumsEventArgs : EventArgs
    {
        public SiteAlbumsEventArgs(ObservableCollection<site_albums> sa)
        {
            _result = sa;
        }

        private ObservableCollection<site_albums> _result;
        public ObservableCollection<site_albums> result { get { return _result; } }
    }


    public partial class apitxt : api
    {

        public event SiteAlbumsEventHandler SiteAlbumsCompleted;


        /// <summary>Permet de récuperer la liste de tout les albums d'un site de famille</summary>
        /// <c>http://apitxt.hellotipi.com/photo/site_albums.json?site=demo</c>
        /// <param name="site">Site de famille dont la liste des albums est à recupérer</param>
        /// <returns>La liste des albums du site de famille <paramref name="site"/></returns>
        public void site_albums(string site)
        {
            //return getList<site_albums>(String.Format("photo/site_albums.json?site={0}", site));

            base.APICallCompleted += new APICallCompletedEventHandler(SiteAlbumsCallCompleted);

            var querystring = new List<KeyValuePair<string, string>>
                             {
                                 new KeyValuePair<string, string>("site", site)
                             };

            GetUrlApi("photo/site_albums.json", querystring);

        }

        void SiteAlbumsCallCompleted(object sender, ApiOpenReadCompletedEventArgs e)
        {
            try
            {
                SiteAlbumsCompleted.Invoke(sender, new SiteAlbumsEventArgs(serializeTools.Deserialize<ObservableCollection<site_albums>>(e.Result)));
            }
            finally
            {
                base.APICallCompleted -= new APICallCompletedEventHandler(SiteAlbumsCallCompleted);
            }
        }

    }
}
